<template>
  <view class="x-select-plus">
    <!-- 下拉选择框 -->
    <view class="select-box" @click="toggleDropdown">
      <input
        v-model="selectedOption"
        placeholder="请选择或输入"
        readonly
        class="select-input"
      />
      <uni-icons type="arrowdown" size="16" class="arrow-icon" :class="{ 'rotate': isOpen }"></uni-icons>
    </view>

    <!-- 下拉菜单 -->
    <view class="dropdown" v-if="isOpen">
      <!-- 输入添加新选项 -->
      <view class="add-option">
        <input
          v-model="inputValue"
          placeholder="输入新选项"
          @keyup.enter="addOption"
        />
        <view class="add-btn" @click="addOption">
          <view class="add-icon"></view>
        </view>
      </view>

      <!-- 选项列表 -->
      <scroll-view scroll-y class="options-list">
        <view
          v-for="(option, index) in options"
          :key="index"
          class="option-item"
          @click="selectOption(option)"
        >
          <text>{{ option }}</text>
          <view class="delete-btn" @click.stop="removeOption(index)">
            <view class="close-icon"></view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    initialOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      inputValue: '',
      options: [...this.initialOptions],
      isOpen: false,
      selectedOption: ''
    };
  },
  watch: {
    initialOptions: {
      immediate: true,
      handler(newVal) {
        this.options = [...newVal];
      }
    }
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen;
    },
    addOption() {
      if (this.inputValue.trim() && !this.options.includes(this.inputValue)) {
        this.options.push(this.inputValue);
        this.inputValue = '';
      }
    },
    removeOption(index) {
      this.options.splice(index, 1);
    },
    selectOption(option) {
      this.selectedOption = option;
      this.isOpen = false;
    }
  }
};
</script>

<style scoped>
.x-select-plus {
  position: relative;
  width: 100%;
  max-width: 300px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

.select-box {
  position: relative;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  padding: 10px 12px;
  background-color: #fff;
  transition: border-color 0.2s ease-in-out;
  cursor: pointer;
}

.select-box:hover {
  border-color: #409eff;
}

.select-input {
  width: 100%;
  border: none;
  outline: none;
  background-color: transparent;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
}

.arrow-icon {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  transition: transform 0.2s ease-in-out;
  color: #c0c4cc;
}

.arrow-icon.rotate {
  transform: translateY(-50%) rotate(180deg);
}

.dropdown {
  position: absolute;
  width: 100%;
  margin-top: 8px;
  border: 1px solid #dcdfe6;
  border-radius: 8px;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  z-index: 1000;
  overflow: hidden;
}

.add-option {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid #eee;
  background-color: #fafafa;
}

.add-option input {
  flex: 1;
  padding: 6px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
}

.add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 4px;
  background-color: transparent;
  border: 1px solid #dcdfe6;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

.add-btn:hover {
  border-color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
}

.add-icon {
  position: relative;
  width: 14px;
  height: 14px;
}

.add-icon::before,
.add-icon::after {
  content: '';
  position: absolute;
  background-color: #606266;
  transition: all 0.2s ease-in-out;
}

.add-icon::before {
  top: 50%;
  left: 0;
  width: 100%;
  height: 2px;
  transform: translateY(-50%);
}

.add-icon::after {
  top: 0;
  left: 50%;
  width: 2px;
  height: 100%;
  transform: translateX(-50%);
}

.options-list {
  max-height: 200px;
  overflow-y: auto;
}

.option-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: background-color 0.2s ease-in-out;
}

.option-item text {
  flex: 1;
  margin-right: 8px;
}

.delete-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: transparent;
  border: 1px solid #dcdfe6;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
}

.delete-btn:hover {
  border-color: #f56c6c;
  background-color: rgba(245, 108, 108, 0.1);
}

.close-icon {
  position: relative;
  width: 12px;
  height: 12px;
}

.close-icon::before,
.close-icon::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 2px;
  background-color: #606266;
  transition: all 0.2s ease-in-out;
}

.close-icon::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.close-icon::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}
</style> 